package com.example.demo.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;

/*
全局异常处理
 */
@ControllerAdvice(annotations = {RestController.class,Controller.class})
@ResponseBody
@Slf4j
public class GlobaExceptioonHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public S<String> exceptionHander(SQLIntegrityConstraintViolationException ex){
        log.info(ex.getMessage());
        String[] s = ex.getMessage().split(" ");

        return S.error("失败了"+s[2]);
    }
    @ExceptionHandler(CoustomException.class)
    public S<String> exceptionHander(CoustomException ex){
        log.info(ex.getMessage());
        return S.error(ex.getMessage());
    }
}
